//=============================================================================
//
//		Copyright (C) 1995, 1996 by Paul S. McCarthy and Eric Sunshine.
//				Written by Paul S. McCarthy and Eric Sunshine.
//							All Rights Reserved.
//
//		This notice may not be removed from this source code.
//
//		This object is included in the MiscKit by permission from the authors
//		and its use is governed by the MiscKit license, found in the file
//		"License.rtf" in the MiscKit distribution.	Please refer to that file
//		for a list of all applicable permissions and restrictions.
//		
//=============================================================================
//-----------------------------------------------------------------------------
// MiscTableCell.M
//
//		Default cell class used by MiscTableScroll to display text.
//
// NOTE *SET-OWNER-VALUE*
//		We must implement these methods so that MiscTableScroll will call 
//		these methods instead of the explicit instance -setXxx methods.	 
//		However, we don't need to do any work.	We will ask the owner for 
//		it's value whenever we need it.	 
//
// FIXME: optional-allocation stuff does not address alignment requirements.
// FIXME: automate most of the optional-allocation stuff.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// $Id: MiscTableCell.M,v 1.7 96/01/13 23:41:21 zarnuk Exp $
// $Log:		MiscTableCell.M,v $
// Revision 1.7	 96/01/13  23:41:21	 zarnuk
// Tag is no longer optional.
// 
// Revision 1.6	 95/10/14  16:48:53	 zarnuk
// Now calls -setUseOwnerFont:YES in -initTextCell:
// 
// Revision 1.5	 95/10/03  05:01:54	 sunshine
// Implemented -copyFromZone: instead of -copy.	 Now all allocations are made
// from [self zone] instead of default-malloc-zone.
// 
// Revision 1.4	 95/10/01  15:07:07	 sunshine
// Added highlightTextColor and highlightBackgroundColor and corresponding
// manipulation methods and 'gray' methods.	 Fixed a bunch of bugs.	 For
// instance the -init methods were unconditionally setting the font and color
// (and allocated memory for them) and clearing the useOwner... stuff.
// Simplified -*pos methods somewhat by removing copy/paste code.  Now each
// one calls its predecessor.  Made default background color LTGRAY once again
// because Paul hatez it and it's much more consistent with the rest of the
// AppKit (every scrolling list in practically every application has a LTGRAY
// background).
// 
// Revision 1.3	 95/09/29  16:01:02	 zarnuk
// lazy-allocation for tags, colors.
// useOwner options for font, colors.
//-----------------------------------------------------------------------------
#import <misckit/MiscTableCell.h>

extern "Objective-C" {
#import <appkit/Font.h>
#import <appkit/Text.h>
#import <dpsclient/wraps.h>
}

extern "C" {
#import <string.h>
}

#define MISC_TC_VERSION_0		0
#define MISC_TC_VERSION_1		1
#define MISC_TC_VERSION			MISC_TC_VERSION_1

@implementation MiscTableCell

//-----------------------------------------------------------------------------
// + initialize
//-----------------------------------------------------------------------------
+ initialize
	{
	if (self == [MiscTableCell class])
		{
		[self setVersion: MISC_TC_VERSION];
		}
	return self;
	}


//-----------------------------------------------------------------------------
// + defaultFont
//-----------------------------------------------------------------------------
+ defaultFont
	{
	return [Font userFontOfSize:12.0 matrix:NX_FLIPPEDMATRIX];
	}


//-----------------------------------------------------------------------------
// initTextCell:
//		NOTE *1*  -[Cell initTextCell:] invokes -setStringValue: which
//		invokes -setFont:.	This means that this cell will get an explicit
//		-setFont call before we finish initialization, and we need to 
//		restore the useOwnerFont setting.
//-----------------------------------------------------------------------------
- initTextCell: (char const*) s
	{
	[super initTextCell:s];								// NOTE *1*
	[self setBordered:NO];
	[self setWrap:NO];
	[self setAlignment:NX_LEFTALIGNED];
	[self setUseOwnerFont:YES];							// NOTE *1*
	[self setOwnerFont: [[self class] defaultFont]];
	return self;
	}


//-----------------------------------------------------------------------------
// initIconCell:
//-----------------------------------------------------------------------------
- initIconCell: (char const*) s
	{
	[super initIconCell:s];
	[self setBordered:NO];
	return self;
	}


//-----------------------------------------------------------------------------
// tag
//-----------------------------------------------------------------------------
- (int) tag
	{
	return tag;
	}


//-----------------------------------------------------------------------------
// setTag:
//-----------------------------------------------------------------------------
- setTag:(int)x
	{
	tag = x;
	return self;
	}


//-----------------------------------------------------------------------------
// image
//-----------------------------------------------------------------------------
- image
	{
	return ([self type] == NX_ICONCELL ? support : 0);
	}


//-----------------------------------------------------------------------------
// setImage:
//-----------------------------------------------------------------------------
- setImage: image
	{
	if ([self type] == NX_ICONCELL)
		{
		char const* name = [image name];
		if (name)
			{
			[self setIcon: name];
			}
		else
			{
			support = image;
			if (contents != 0 && cFlags1.freeText)
				NXZoneFree( [self zone], contents );
			contents = 0;
			cFlags1.freeText = NO;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// owner
//-----------------------------------------------------------------------------
- owner
	{
	return owner;
	}


//-----------------------------------------------------------------------------
// setOwner:
//-----------------------------------------------------------------------------
- setOwner:obj
	{
	if (obj != owner)
		{
		owner = obj;
		if (owner != 0)
			{
			if ([owner respondsTo:@selector(font)])
				[self setOwnerFont:[owner font]];
			if ([owner respondsTo:@selector(textColor)])
				[self setOwnerTextColor:[owner textColor]];
			if ([owner respondsTo:@selector(backgroundColor)])
				[self setOwnerBackgroundColor:[owner backgroundColor]];
			if ([owner respondsTo:@selector(highlightTextColor)])
				[self setOwnerTextColor:[owner highlightTextColor]];
			if ([owner respondsTo:@selector(highlightBackgroundColor)])
				[self setOwnerBackgroundColor:[owner highlightBackgroundColor]];
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// font
//-----------------------------------------------------------------------------
- font
	{
	return [super font];
	}


//-----------------------------------------------------------------------------
// setFont:
//-----------------------------------------------------------------------------
- setFont:fontObj
	{
	[self setUseOwnerFont:NO];
	[super setFont:fontObj];
	return self;
	}


//-----------------------------------------------------------------------------
// tc1Flags
//-----------------------------------------------------------------------------
- (unsigned int) tc1Flags
	{
	return tc1_flags;
	}


//-----------------------------------------------------------------------------
// tc1DataSize
//-----------------------------------------------------------------------------
- (unsigned int) tc1DataSize
	{
	unsigned int size = 0;
	if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR)
		size += [self tc1TextColorLen];
	if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR)
		size += [self tc1BackgroundColorLen];
	if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H)
		size += [self tc1HighlightTextColorLen];
	if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR_H)
		size += [self tc1HighlightBackgroundColorLen];
	return size;
	}


//-----------------------------------------------------------------------------
// tc1TextColorPos
//-----------------------------------------------------------------------------
- (unsigned int) tc1TextColorPos
	{
	return 0;
	}


//-----------------------------------------------------------------------------
// tc1BackgroundColorPos
//-----------------------------------------------------------------------------
- (unsigned int) tc1BackgroundColorPos
	{
	unsigned int pos = [self tc1TextColorPos];
	if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR)
		pos += [self tc1TextColorLen];
	return pos;
	}


//-----------------------------------------------------------------------------
// tc1HighlightTextColorPos
//-----------------------------------------------------------------------------
- (unsigned int) tc1HighlightTextColorPos
	{
	unsigned int pos = [self tc1BackgroundColorPos];
	if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR)
		pos += [self tc1BackgroundColorLen];
	return pos;
	}


//-----------------------------------------------------------------------------
// tc1HighlightBackgroundColorPos
//-----------------------------------------------------------------------------
- (unsigned int) tc1HighlightBackgroundColorPos
	{
	unsigned int pos = [self tc1HighlightTextColorPos];
	if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H)
		pos += [self tc1HighlightTextColorLen];
	return pos;
	}


//-----------------------------------------------------------------------------
// tc1TextColorLen
//-----------------------------------------------------------------------------
- (unsigned int) tc1TextColorLen
	{
	return sizeof(NXColor);
	}


//-----------------------------------------------------------------------------
// tc1BackgroundColorLen
//-----------------------------------------------------------------------------
- (unsigned int) tc1BackgroundColorLen
	{
	return sizeof(NXColor);
	}


//-----------------------------------------------------------------------------
// tc1HighlightTextColorLen
//-----------------------------------------------------------------------------
- (unsigned int) tc1HighlightTextColorLen
	{
	return sizeof(NXColor);
	}


//-----------------------------------------------------------------------------
// tc1HighlightBackgroundColorLen
//-----------------------------------------------------------------------------
- (unsigned int) tc1HighlightBackgroundColorLen
	{
	return sizeof(NXColor);
	}


//-----------------------------------------------------------------------------
// tc1TextColorPtr
//-----------------------------------------------------------------------------
- (NXColor*) tc1TextColorPtr
	{
	return (NXColor*) ((char*)tc1_data + [self tc1TextColorPos]);
	}


//-----------------------------------------------------------------------------
// tc1BackgroundColorPtr
//-----------------------------------------------------------------------------
- (NXColor*) tc1BackgroundColorPtr
	{
	return (NXColor*) ((char*)tc1_data + [self tc1BackgroundColorPos]);
	}


//-----------------------------------------------------------------------------
// tc1HighlightTextColorPtr
//-----------------------------------------------------------------------------
- (NXColor*) tc1HighlightTextColorPtr
	{
	return (NXColor*) ((char*)tc1_data + [self tc1HighlightTextColorPos]);
	}


//-----------------------------------------------------------------------------
// tc1HighlightBackgroundColorPtr
//-----------------------------------------------------------------------------
- (NXColor*) tc1HighlightBackgroundColorPtr
	{
	return (NXColor*) ((char*)tc1_data + [self tc1HighlightBackgroundColorPos]);
	}


//-----------------------------------------------------------------------------
// tc1InsertData:len:pos:
//-----------------------------------------------------------------------------
- (void*) tc1InsertData:(void const*)data
		pos:(unsigned int)pos
		len:(unsigned int)len
	{
	if (len > 0)
		{
		unsigned int const old_size = [self tc1DataSize];
		unsigned int const new_size = old_size + len;
		if (old_size == 0)
			tc1_data = NXZoneMalloc( [self zone], new_size );
		else
			{
			tc1_data = NXZoneRealloc( [self zone], tc1_data, new_size );
			if (pos < old_size)
				memmove( (char*) tc1_data + pos + len,			// Destination
						(char*) tc1_data + pos,					// Source
						old_size - pos );						// # bytes.
			}
		if (data != 0)
			memcpy( (char*) tc1_data + pos, data, len );
		else
			memset( (char*) tc1_data + pos, 0, len );
		return (void*)((char*) tc1_data + pos );
		}
	return 0;
	}


//-----------------------------------------------------------------------------
// tc1DeleteDataPos:len:
//-----------------------------------------------------------------------------
- (void) tc1DeleteDataPos:(unsigned int)pos len:(unsigned int)len
	{
	if (len > 0)
		{
		unsigned int const old_size = [self tc1DataSize];
		unsigned int const new_size = old_size - len;
		if (new_size == 0)
			{
			NXZoneFree( [self zone], tc1_data );
			tc1_data = 0;
			}
		else
			{
			if (pos < new_size)
				memmove( (char*) tc1_data + pos,				// Destination
						(char*) tc1_data + pos + len,			// Source
						new_size - pos );						// # bytes
			tc1_data = NXZoneRealloc( [self zone], tc1_data, new_size );
			}
		}
	}


//-----------------------------------------------------------------------------
// useOwnerFont
//-----------------------------------------------------------------------------
- (BOOL) useOwnerFont
	{
	return ((tc1_flags & MISC_TC1_SELF_FONT) == 0);
	}


//-----------------------------------------------------------------------------
// useOwnerTextColor
//-----------------------------------------------------------------------------
- (BOOL) useOwnerTextColor
	{
	return ((tc1_flags & MISC_TC1_SELF_TEXT_COLOR) == 0);
	}


//-----------------------------------------------------------------------------
// useOwnerBackgroundColor
//-----------------------------------------------------------------------------
- (BOOL) useOwnerBackgroundColor
	{
	return ((tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR) == 0);
	}


//-----------------------------------------------------------------------------
// useOwnerHighlightTextColor
//-----------------------------------------------------------------------------
- (BOOL) useOwnerHighlightTextColor
	{
	return ((tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H) == 0);
	}


//-----------------------------------------------------------------------------
// useOwnerHighlightBackgroundColor
//-----------------------------------------------------------------------------
- (BOOL) useOwnerHighlightBackgroundColor
	{
	return ((tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR_H) == 0);
	}


//-----------------------------------------------------------------------------
// setUseOwnerFont:
//-----------------------------------------------------------------------------
- setUseOwnerFont: (BOOL) flag
	{
	if ([self useOwnerFont] != flag)
		{
		if (flag)
			{
			tc1_flags &= ~(MISC_TC1_SELF_FONT);
			if (owner != 0 && [owner respondsTo:@selector(font)])
				[self setOwnerFont:[owner font]];
			}
		else // (!flag)
			{
			tc1_flags |= MISC_TC1_SELF_FONT;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// setUseOwnerTextColor:
//-----------------------------------------------------------------------------
- setUseOwnerTextColor: (BOOL) flag
	{
	if ([self useOwnerTextColor] != flag)
		{
		unsigned int const pos = [self tc1TextColorPos];
		unsigned int const len = [self tc1TextColorLen];
		if (flag)
			{
			[self tc1DeleteDataPos:pos len:len];
			tc1_flags &= ~(MISC_TC1_SELF_TEXT_COLOR);
			}
		else // (!flag)
			{
			NXColor color = [[self class] defaultTextColor];
			[self tc1InsertData:&color pos:pos len:len];
			tc1_flags |= MISC_TC1_SELF_TEXT_COLOR;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// setUseOwnerBackgroundColor:
//-----------------------------------------------------------------------------
- setUseOwnerBackgroundColor: (BOOL) flag
	{
	if ([self useOwnerBackgroundColor] != flag)
		{
		unsigned int const pos = [self tc1BackgroundColorPos];
		unsigned int const len = [self tc1BackgroundColorLen];
		if (flag)
			{
			[self tc1DeleteDataPos:pos len:len];
			tc1_flags &= ~(MISC_TC1_SELF_BACKGROUND_COLOR);
			}
		else // (!flag)
			{
			NXColor color = [[self class] defaultBackgroundColor];
			[self tc1InsertData:&color pos:pos len:len];
			tc1_flags |= MISC_TC1_SELF_BACKGROUND_COLOR;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// setUseOwnerHighlightTextColor:
//-----------------------------------------------------------------------------
- setUseOwnerHighlightTextColor: (BOOL) flag
	{
	if ([self useOwnerHighlightTextColor] != flag)
		{
		unsigned int const pos = [self tc1HighlightTextColorPos];
		unsigned int const len = [self tc1HighlightTextColorLen];
		if (flag)
			{
			[self tc1DeleteDataPos:pos len:len];
			tc1_flags &= ~(MISC_TC1_SELF_TEXT_COLOR_H);
			}
		else // (!flag)
			{
			NXColor color = [[self class] defaultHighlightTextColor];
			[self tc1InsertData:&color pos:pos len:len];
			tc1_flags |= MISC_TC1_SELF_TEXT_COLOR_H;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// setUseOwnerHighlightBackgroundColor:
//-----------------------------------------------------------------------------
- setUseOwnerHighlightBackgroundColor: (BOOL) flag
	{
	if ([self useOwnerHighlightBackgroundColor] != flag)
		{
		unsigned int const pos = [self tc1HighlightBackgroundColorPos];
		unsigned int const len = [self tc1HighlightBackgroundColorLen];
		if (flag)
			{
			[self tc1DeleteDataPos:pos len:len];
			tc1_flags &= ~(MISC_TC1_SELF_BACKGROUND_COLOR_H);
			}
		else // (!flag)
			{
			NXColor color = [[self class] defaultHighlightBackgroundColor];
			[self tc1InsertData:&color pos:pos len:len];
			tc1_flags |= MISC_TC1_SELF_BACKGROUND_COLOR_H;
			}
		}
	return self;
	}


//-----------------------------------------------------------------------------
// setOwnerFont:
//-----------------------------------------------------------------------------
- setOwnerFont:fontObj
	{
	if ([self useOwnerFont])
		[super setFont:fontObj];
	return self;
	}


//-----------------------------------------------------------------------------
// setOwner values...	NOTE *SET-OWNER-VALUE*
//-----------------------------------------------------------------------------

- setOwnerTextColor: (NXColor) pcolor					{ return self; }
- setOwnerBackgroundColor: (NXColor) pcolor				{ return self; }
- setOwnerHighlightTextColor: (NXColor) pcolor			{ return self; }
- setOwnerHighlightBackgroundColor: (NXColor) pcolor	{ return self; }


//-----------------------------------------------------------------------------
// textGray
//-----------------------------------------------------------------------------
- (float)textGray
	{
	float gray;
	NXConvertColorToGray( [self textColor], &gray );
	return gray;
	}


//-----------------------------------------------------------------------------
// setTextGray:
//-----------------------------------------------------------------------------
- setTextGray:(float)value
	{
	return [self setTextColor: NXConvertGrayToColor( value )];
	}


//-----------------------------------------------------------------------------
// backgroundGray
//-----------------------------------------------------------------------------
- (float)backgroundGray
	{
	float gray;
	NXConvertColorToGray( [self backgroundColor], &gray );
	return gray;
	}


//-----------------------------------------------------------------------------
// setBackgroundGray:
//-----------------------------------------------------------------------------
- setBackgroundGray:(float)value
	{
	return [self setBackgroundColor: NXConvertGrayToColor( value ) ];
	}


//-----------------------------------------------------------------------------
// highlightTextGray
//-----------------------------------------------------------------------------
- (float)highlightTextGray
	{
	float gray;
	NXConvertColorToGray( [self highlightTextColor], &gray );
	return gray;
	}


//-----------------------------------------------------------------------------
// setHighlightTextGray:
//-----------------------------------------------------------------------------
- setHighlightTextGray:(float)value
	{
	return [self setHighlightTextColor: NXConvertGrayToColor( value )];
	}


//-----------------------------------------------------------------------------
// highlightBackgroundGray
//-----------------------------------------------------------------------------
- (float)highlightBackgroundGray
	{
	float gray;
	NXConvertColorToGray( [self highlightBackgroundColor], &gray );
	return gray;
	}


//-----------------------------------------------------------------------------
// setHighlightBackgroundGray:
//-----------------------------------------------------------------------------
- setHighlightBackgroundGray:(float)value
	{
	return [self setHighlightBackgroundColor: NXConvertGrayToColor( value ) ];
	}


//-----------------------------------------------------------------------------
// +defaultBackgroundColor
//-----------------------------------------------------------------------------
+ (NXColor) defaultBackgroundColor
	{
	return NX_COLORLTGRAY;
	}


//-----------------------------------------------------------------------------
// backgroundColor
//-----------------------------------------------------------------------------
- (NXColor) backgroundColor
	{
	if ([self useOwnerBackgroundColor])
		{
		if (owner != 0 && [owner respondsTo:@selector(backgroundColor)])
			return [owner backgroundColor];
		return [[self class] defaultBackgroundColor];
		}
	NXColor const* const p = [self tc1BackgroundColorPtr];
	return *p;
	}


//-----------------------------------------------------------------------------
// setBackgroundColor
//-----------------------------------------------------------------------------
- setBackgroundColor: (NXColor) c
	{
	NXColor* p;
	[self setUseOwnerBackgroundColor:NO];
	p = [self tc1BackgroundColorPtr];
	*p = c;
	return self;
	}


//-----------------------------------------------------------------------------
// +defaultHighlightBackgroundColor
//-----------------------------------------------------------------------------
+ (NXColor) defaultHighlightBackgroundColor
	{
	return NX_COLORWHITE;
	}


//-----------------------------------------------------------------------------
// highlightBackgroundColor
//-----------------------------------------------------------------------------
- (NXColor) highlightBackgroundColor
	{
	if ([self useOwnerHighlightBackgroundColor])
		{
		if (owner && [owner respondsTo:@selector(highlightBackgroundColor)])
			return [owner highlightBackgroundColor];
		return [[self class] defaultHighlightBackgroundColor];
		}
	NXColor const* const p = [self tc1HighlightBackgroundColorPtr];
	return *p;
	}


//-----------------------------------------------------------------------------
// setHighlightBackgroundColor
//-----------------------------------------------------------------------------
- setHighlightBackgroundColor: (NXColor) c
	{
	NXColor* p;
	[self setUseOwnerHighlightBackgroundColor:NO];
	p = [self tc1HighlightBackgroundColorPtr];
	*p = c;
	return self;
	}


//-----------------------------------------------------------------------------
// +defaultTextColor
//-----------------------------------------------------------------------------
+ (NXColor) defaultTextColor
	{
	return NX_COLORBLACK;
	}


//-----------------------------------------------------------------------------
// textColor
//-----------------------------------------------------------------------------
- (NXColor) textColor
	{
	if ([self useOwnerTextColor])
		{
		if (owner != 0 && [owner respondsTo:@selector(textColor)])
			return [owner textColor];
		return [[self class] defaultTextColor];
		}
	NXColor const* const p = [self tc1TextColorPtr];
	return *p;
	}


//-----------------------------------------------------------------------------
// setTextColor
//-----------------------------------------------------------------------------
- setTextColor: (NXColor) c
	{
	NXColor* p;
	[self setUseOwnerTextColor:NO];
	p = [self tc1TextColorPtr];
	*p = c;
	return self;
	}


//-----------------------------------------------------------------------------
// +defaultHighlightTextColor
//-----------------------------------------------------------------------------
+ (NXColor) defaultHighlightTextColor
	{
	return NX_COLORBLACK;
	}


//-----------------------------------------------------------------------------
// highlightTextColor
//-----------------------------------------------------------------------------
- (NXColor) highlightTextColor
	{
	if ([self useOwnerHighlightTextColor])
		{
		if (owner != 0 && [owner respondsTo:@selector(highlightTextColor)])
			return [owner highlightTextColor];
		return [[self class] defaultHighlightTextColor];
		}
	NXColor const* const p = [self tc1HighlightTextColorPtr];
	return *p;
	}


//-----------------------------------------------------------------------------
// setHighlightTextColor
//-----------------------------------------------------------------------------
- setHighlightTextColor: (NXColor) c
	{
	NXColor* p;
	[self setUseOwnerHighlightTextColor:NO];
	p = [self tc1HighlightTextColorPtr];
	*p = c;
	return self;
	}


//-----------------------------------------------------------------------------
// bgColor
//-----------------------------------------------------------------------------
- (NXColor) bgColor
	{
	// FIXME: Use [self isHighlighted] when we figure out how to support
	// this for lazy mode.
	return cFlags1.state ?
				[self highlightBackgroundColor] : [self backgroundColor];
	}


//-----------------------------------------------------------------------------
// fgColor
//-----------------------------------------------------------------------------
- (NXColor) fgColor
	{
	// FIXME: Use [self isHighlighted] when we figure out how to support
	// this for lazy mode.
	return cFlags1.state ? [self highlightTextColor] : [self textColor];
	}


//-----------------------------------------------------------------------------
// calcCellSize:inRect:
//-----------------------------------------------------------------------------
- calcCellSize: (NXSize*) theSize inRect: (NXRect const*) aRect
	{
	[super calcCellSize: theSize inRect: aRect];
	theSize->width += 1;
	theSize->height += 1;
	return self;
	}


//-----------------------------------------------------------------------------
// setTextAttributes:
//-----------------------------------------------------------------------------
- setTextAttributes:textObject
	{
	[super setTextAttributes: textObject];
	[textObject setTextColor: [self fgColor]];
	[textObject setBackgroundColor: [self bgColor]];
	return textObject;
	}


//-----------------------------------------------------------------------------
// drawInside:inView:
//-----------------------------------------------------------------------------
- drawInside:(NXRect const*)cellFrame inView:controlView
	{
	NXRect rect;

	NXSetColor( [self bgColor] );
	NXSetRect( &rect, NX_X(cellFrame), NX_Y(cellFrame),
				   NX_WIDTH(cellFrame) - 1, NX_HEIGHT(cellFrame) - 1 );
	NXRectFill( &rect );
	[super drawInside:&rect inView:controlView];
	return self;
	}


//-----------------------------------------------------------------------------
// drawSelf:inView:
//-----------------------------------------------------------------------------
- drawSelf:(NXRect const*)cellFrame inView:aView
	{
	float const MEDGRAY = 0.5;
	NXRect r[2];

	PSsetgray( MEDGRAY );
	NXSetRect( &(r[0]), NX_MAXX(cellFrame) - 1, NX_Y(cellFrame),
						1, NX_HEIGHT(cellFrame) );
	NXSetRect( &(r[1]), NX_X(cellFrame), NX_MAXY(cellFrame) - 1,
						NX_WIDTH(cellFrame), 1 );
	NXRectFillList( r, 2 );

	return [self drawInside:cellFrame inView:aView];
	}


//-----------------------------------------------------------------------------
// tc1DestroyData
//-----------------------------------------------------------------------------
- (void) tc1DestroyData
	{
	tc1_flags = 0;
	}


//-----------------------------------------------------------------------------
// tc1FreeData
//-----------------------------------------------------------------------------
- (void) tc1FreeData
	{
	if (tc1_data != 0)
		{
		NXZoneFree( [self zone], tc1_data );
		tc1_data = 0;
		}
	}


//-----------------------------------------------------------------------------
// free
//-----------------------------------------------------------------------------
- free
	{
	[self tc1DestroyData];
	[self tc1FreeData];
	return [super free];
	}


//-----------------------------------------------------------------------------
// copyFromZone:
//-----------------------------------------------------------------------------
- copyFromZone: (NXZone*) zone;
	{
	MiscTableCell* the_clone = [super copyFromZone:zone];
	if (tc1_data != 0)
		{
		unsigned int const size = [self tc1DataSize];
		void* p = NXZoneMalloc( [self zone], size );
		memcpy( p, tc1_data, size );
		the_clone->tc1_data = p;
		}
	return the_clone;
	}


//-----------------------------------------------------------------------------
// read:
//-----------------------------------------------------------------------------
- read: (NXTypedStream*) stream
	{
	[super read:stream];

	[self tc1DestroyData];
	[self tc1FreeData];

	unsigned int x;
	int ver = NXTypedStreamClassVersion( stream, [[MiscTableCell class] name] );

	owner = NXReadObject( stream );

	if (ver == MISC_TC_VERSION_0)
		{
		NXReadType( stream, @encode(unsigned int), &x );
		if (x & MISC_TC1_HAS_TAG)
			NXReadType( stream, @encode(int), &tag );
		else
			tag = 0;
		}
	else if (ver == MISC_TC_VERSION_1)
		{
		NXReadType( stream, @encode(int), &tag );
		NXReadType( stream, @encode(unsigned int), &x );
		}
	else
		{
		[self error:"Cannot read: unknown version."];
		}

	if (x & MISC_TC1_SELF_TEXT_COLOR)
		{
		NXColor c = NXReadColor( stream );
		[self setTextColor:c];
		}
	if (x & MISC_TC1_SELF_BACKGROUND_COLOR)
		{
		NXColor c = NXReadColor( stream );
		[self setBackgroundColor:c];
		}
	if (x & MISC_TC1_SELF_TEXT_COLOR_H)
		{
		NXColor c = NXReadColor( stream );
		[self setHighlightTextColor:c];
		}
	if (x & MISC_TC1_SELF_BACKGROUND_COLOR_H)
		{
		NXColor c = NXReadColor( stream );
		[self setHighlightBackgroundColor:c];
		}
	return self;
	}


//-----------------------------------------------------------------------------
// write:
//-----------------------------------------------------------------------------
- write: (NXTypedStream*) stream
	{
	[super write:stream];
	NXWriteObjectReference( stream, owner );
	NXWriteType( stream, @encode(int), &tag );
	NXWriteType( stream, @encode(unsigned int), &tc1_flags );
	if (![self useOwnerTextColor])
		NXWriteColor( stream, [self textColor] );
	if (![self useOwnerBackgroundColor])
		NXWriteColor( stream, [self backgroundColor] );
	if (![self useOwnerHighlightTextColor])
		NXWriteColor( stream, [self highlightTextColor] );
	if (![self useOwnerHighlightBackgroundColor])
		NXWriteColor( stream, [self highlightBackgroundColor] );
	return self;
	}

@end
